﻿using Itech_Run.Sys.Domain.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Volo.Abp.Modularity;

namespace Itech_Run.Sys.EntityFrameworkCore
{
    [DependsOn(typeof(Itech_Run.EntityFrameworkCore.Shared.Itech_RunEntityFrameworkCoreSharedModule),
        typeof(Sys.Domain.Itech_RunSysDomainModule))]
    public class Itech_RunSysEntityFrameworkCoreModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddAbpDbContext<Itech_RunSysContext>(options =>
            {
                /* Remove "includeAllEntities: true" to create
                 * default repositories only for aggregate roots */
                options.AddDefaultRepositories(includeAllEntities: true);
            });

            context.Services.Configure<AbpDbContextOptions>(options => options.UseSqlServer());
            configure();
        }

        private void configure()
        {
            Configure<AbpEntityOptions>(options =>
            {
                options.Entity<Sys_User>(options =>
                {
                    options.DefaultWithDetailsFunc = query =>
                    {
                        query = query.Include(o => o.Sys_UserRole).ThenInclude(o => o.Role);
                        query = query.Include(o => o.Sys_Resume);
                        return query;
                    };
                });

                options.Entity<Sys_Module>(options =>
                {
                    options.DefaultWithDetailsFunc = query =>
                    {
                        query = query.Include(o => o.Sys_ModuleButton).ThenInclude(o => o.Button);
                        query = query.Include(o => o.Parent);
                        query = query.Include(o => o.Childs);
                        query = query.Include(o => o.Sys_RoleModuleButton);
                        return query;
                    };
                });

                options.Entity<Sys_RoleModuleButton>(options =>
                {
                    options.DefaultWithDetailsFunc = query =>
                    {
                        query = query.Include(o => o.Role);
                        return query;
                    };
                });

                options.Entity<Sys_RoleModule>(options =>
                {
                    options.DefaultWithDetailsFunc = query =>
                    {
                        query = query.Include(o => o.Role);
                        return query;
                    };
                });

                options.Entity<Sys_Role>(options =>
                {
                    options.DefaultWithDetailsFunc = query =>
                    {
                        query = query.Include(o => o.Sys_RoleModule);
                        query = query.Include(o => o.Sys_RoleModuleButton);
                        return query;
                    };
                });
            });
        }
    }
}